package stack.ExerciseClass;

import java.util.Scanner;

public class Exercise3{
    public static void main(String[] args) {
        Linked linked = new Linked();
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入S表示退出输入：");
        while (true){
            System.out.println("输入符号：");
            String str = scanner.next();

            if(str.length()>1){
                System.out.println("输入错误");
                continue;
            }
            Char c1 = new Char(str);
            if("(".equals(str)){
                linked.pushData(c1);
            }
            if(")".equals(str)){
                linked.popData();
            }
            if(("s".equalsIgnoreCase(str))){
                break;
            }
        }
        if (linked.getHead().getNext()==null){
            System.out.println("匹配成功");
        }else {
            System.out.println("匹配失败");
        }
    }
}
class Char{

    private String _char;
    private Char next;
    public Char() {

    }

    public Char(String _char) {
        this._char = _char;
    }

    public String get_char() {
        return _char;
    }

    public void set_char(String _char) {
        this._char = _char;
    }

    public Char getNext() {
        return next;
    }

    public void setNext(Char next) {
        this.next = next;
    }
}
class Linked{
    private Char head= new Char();

    public Char getHead() {
        return head;
    }

    private Char temp;
    public void pushData(Char data){
        if(head.getNext()==null){
            head.setNext(data);
            temp=data;
//            System.out.println("111"+temp.get_char());
            return;
        }
        data.setNext(temp);
        temp=data;
        head.setNext(data);
    }
    public void popData(){
        if(head.getNext()==null){
            System.out.println("栈空");
            return;
        }
        System.out.println("弹出的数据为:"+head.getNext().get_char());
        head.setNext(head.getNext().getNext());
    }
    public void showList(){
        if(head.getNext()==null){
            System.out.println("链表为空");
        }
        //因为头节点是不能动的 所以需要临时遍历辅助
        Char temp = head;
        while (true){
            temp=temp.getNext();
            if (temp==null){
                break;
            }
            System.out.println(temp.get_char());
        }
        System.out.println("输出完毕");
    }
}
